package com.taocyb.core.uc.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taocyb.commons.uc.entity.User;
import com.taocyb.commons.uc.entity.UserDetail;
import com.taocyb.commons.uc.service.UserService;
import com.taocyb.core.uc.manager.UserManager;
import com.taocyb.framework.utils.SecurityUtils;

@Service("userService")
public class UserServiceImpl implements UserService{

	@Autowired
	private UserManager userManager;
	
	@Override
	public User findUserById(Long id) {
		return userManager.findUserById(id);
	}

	@Override
	public User findUserByLoginName(String loginName) {
		return userManager.findUserByLoginName(loginName);
	}
	
	@Override
	public Long saveUser(User user, UserDetail userDetail){
		//昵称默认为用户账号
		if(StringUtils.isBlank(user.getNickName())){
			user.setNickName(user.getLoginName());
		}
		
		String salt = SecurityUtils.hexSalt();
		String password = SecurityUtils.sha1Password(user.getPassword(), salt);
		
		user.setSalt(salt);
		user.setPassword(password);
		
		return userManager.saveUser(user, userDetail);
	}
	
	@Override
	public void updateUser(User user){
		userManager.updateUser(user);
	}

	@Override
	public UserDetail findUserDetailByLoginName(String loginName) {
		return userManager.findUserDetailByLoginName(loginName);
	}
}
